package br.com.cardoso.videostore.persistence;

import java.util.List;

import javax.persistence.Query;

import org.cardoso.persistence.PersistenceDefault;
import org.springframework.stereotype.Repository;

import br.com.cardoso.videostore.model.entity.UserEntity;

@Repository("userPersistence")
public class UserPersistence extends PersistenceDefault<UserEntity> implements IUserPersistence {

	private static final long serialVersionUID = 1L;
	
	public UserPersistence() {
		super(UserEntity.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserEntity getUser(String userName, String password) {
		
		StringBuilder hql = new StringBuilder();
		hql.append(" select user from UserEntity user ");
		hql.append("	where user.userName = :userName ");
		hql.append("		and user.password = :password ");
		
		Query query = super.entityManager.createQuery(hql.toString());
		query.setParameter("userName", userName);
		query.setParameter("password", password);
		
		List<UserEntity> list = query.getResultList();
		
		return (list != null && !list.isEmpty()) ? list.get(0) : null;
	}

}
